package com.guigu.service_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.common_util.pojo.utils.Result;
import com.guigu.service_user.entity.TbAddress;
import com.guigu.service_user.entity.TbAreas;
import com.guigu.service_user.entity.TbCities;
import com.guigu.service_user.entity.TbProvinces;
import com.guigu.service_user.mapper.TbAddressMapper;
import com.guigu.service_user.mapper.TbAreasMapper;
import com.guigu.service_user.mapper.TbCitiesMapper;
import com.guigu.service_user.mapper.TbProvincesMapper;
import com.guigu.service_user.service.ITbAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-07-09
 */
@Service
public class TbAddressServiceImpl extends ServiceImpl<TbAddressMapper, TbAddress> implements ITbAddressService {

    @Autowired
    private TbAddressMapper tbAddressMapper;

    @Autowired
    private TbProvincesMapper tbProvincesMapper;

    @Autowired
    private TbCitiesMapper tbCitiesMapper;

    @Autowired
    private TbAreasMapper tbAreasMapper;

    @Override
    public Result findPage(Map<String, Object> searchMap, Integer page, Integer size) {
        QueryWrapper<TbAddress> wrapper = createExample(searchMap);
        //创建page对象
        Page<TbAddress> pageAlbum = new Page<>(page,size);
        baseMapper.selectPage(pageAlbum,wrapper);
        return Result.ok().data("row",pageAlbum.getRecords()).data("total",pageAlbum.getTotal());
    }

    @Override
    public String provinces(String provincesName) {
        QueryWrapper<TbProvinces> wrapper = new QueryWrapper<>();
        wrapper.eq("province",provincesName);
        TbProvinces tbProvinces = this.tbProvincesMapper.selectOne(wrapper);
        return tbProvinces.getProvinceid();
    }

    @Override
    public String cities(String citiesName) {
        QueryWrapper<TbCities> wrapper = new QueryWrapper<>();
        wrapper.eq("city",citiesName);
        TbCities tbCities = this.tbCitiesMapper.selectOne(wrapper);
        return tbCities.getCityid();
    }

    @Override
    public String areas(String areasName) {
        QueryWrapper<TbAreas> wrapper = new QueryWrapper<>();
        wrapper.eq("area",areasName);
        TbAreas tbAreas = this.tbAreasMapper.selectOne(wrapper);
        return tbAreas.getAreaid();
    }

    @Override
    public List<TbAddress> findByUsername(String username) {
        QueryWrapper<TbAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        List<TbAddress> addressList = baseMapper.selectList(wrapper);
        return addressList;
    }

    // 根据查询条件封装模块对象
    private QueryWrapper<TbAddress> createExample(Map<String, Object> searchMap) {
        QueryWrapper<TbAddress> wrapper = new QueryWrapper<>();
        if(searchMap!=null) {
            if(searchMap.get("id")!=null)
                wrapper.eq("id",searchMap.get("id"));
            if(searchMap.get("username")!=null && !"".equals(searchMap.get("username")))
                wrapper.like("username",searchMap.get("username"));
            if(searchMap.get("alias")!=null && !"".equals(searchMap.get("alias")))
                wrapper.like("alias",searchMap.get("alias"));
        }
        return wrapper;
    }
}
